package dao

import (
	"context"
	"go-shop-admin/internal/model"
	"gorm.io/gorm"
)

type BannerDao struct {
	*gorm.DB
}

// 指定查询的字段
var bannerSelectField = []string{"id", "title", "images"}

// NewBannerDao 把init init_mysql的Db复制过来
func NewBannerDao(ctx context.Context) *BannerDao {
	return &BannerDao{NewDBClient(ctx)}
}

// NewBannerDaoByDB  方法二 把init中的db拿过来  ，当一个函数里，有多个dao ,既有BannerDao，又有GoodsDao ,可用这个方法实现对db的复用,都使用一个db，减少对数据库的链接
func NewBannerDaoByDB(db *gorm.DB) *BannerDao {
	return &BannerDao{db}
}

// GetList 获取列表
func (d *BannerDao) GetList(whereMap WhereStruct) (total int64, object []*model.Banner, err error) {
	d.DB.Model(&model.Banner{}).Where(whereMap.Where).Where(WhereMap).Not(whereMap.NotWhere).Or(whereMap.OrWhere).Count(&total)
	err = d.DB.Model(&model.Banner{}).Select(bannerSelectField).Where(whereMap.Where).Where(WhereMap).Not(whereMap.NotWhere).Or(whereMap.OrWhere).Order(orderCondition).Find(&object).Error //find() 找全部符合条件的
	return
}

// GetById 通过id获取用户信息
func (d *BannerDao) GetById(id uint) (object *model.Banner, err error) {
	err = d.DB.Model(&model.Banner{}).Select(bannerSelectField).Where("id=?", id).First(&object).Error
	return
}
